

import os
import gzip
import shutil
import tarfile
import chardet


# 给定一个文件夹路径A，下面有多个.gz结尾的日志文件包，帮我写一个python脚本把A文件夹下的所有.gz文件解压，并将每个解压后的文件夹中的文件都放到A文件夹下
def extract_gz_files(folder_path):
    # 遍历文件夹中的所有文件
    for filename in os.listdir(folder_path):
        if filename.endswith('.gz'):
            file_path = os.path.join(folder_path, filename)
            # 检查文件是否是tar.gz文件
            if tarfile.is_tarfile(file_path):
                with tarfile.open(file_path, 'r:gz') as tar:
                    tar.extractall(path=folder_path)
            else:
                # 如果是普通的.gz文件，解压到当前文件夹
                with gzip.open(file_path, 'rb') as f_in:
                    with open(os.path.join(folder_path, filename[:-3]), 'wb') as f_out:
                        shutil.copyfileobj(f_in, f_out)
            # 删除原始的.gz文件
            os.remove(file_path)


# 给定一个文件夹路径A，下面有多个.log结尾的日志文件，帮我写一个python脚本在A文件夹下的所有.log文件中查询字符串1："userId": "AD227FF26D771E87E053015B210A5B47"；字符串2："transactorId": "2AC59CCC84B23DF7E063345B210A257C"；字符串3："transactorName": "邹彦斌"；字符串4："identNo": "371002198909151016"；字符串5： "mobilePhone": "13806318956"；并将这五个字符串分别在那个log文件下的第几行
def detect_encoding(file_path):
    with open(file_path, 'rb') as f:
        raw_data = f.read()
        result = chardet.detect(raw_data)
        return result['encoding']


def search_strings_in_logs(folder_path, search_strings):
    # 遍历文件夹中的所有文件
    for filename in os.listdir(folder_path):
        if filename.endswith('.log'):
            file_path = os.path.join(folder_path, filename)
            lines = []
            try:
                # 尝试使用 gbk 编码
                with open(file_path, 'r', encoding='gbk', errors='ignore') as file:
                    lines = file.readlines()
            except UnicodeDecodeError:
                try:
                    # 尝试使用 gb18030 编码
                    with open(file_path, 'r', encoding='gb18030', errors='ignore') as file:
                        lines = file.readlines()
                except UnicodeDecodeError:
                    try:
                        # 尝试使用 latin1 编码
                        with open(file_path, 'r', encoding='latin1', errors='ignore') as file:
                            lines = file.readlines()
                    except UnicodeDecodeError:
                        # 自动检测编码格式
                        encoding = detect_encoding(file_path)
                        with open(file_path, 'r', encoding=encoding, errors='ignore') as file:
                            lines = file.readlines()

            if not lines:
                print(f"文件: {filename} 读取失败或为空")
                continue

            for search_string in search_strings:
                line_numbers = [i + 1 for i, line in enumerate(lines) if search_string in line]
                if line_numbers:
                    print(f"文件: {filename}, 字符串: {search_string}, 行号: {line_numbers}")
                else:
                    print(f"文件: {filename}, 字符串: {search_string} 未找到")




if __name__ == '__main__':
    # # 使用示例
    # folder_path = r'C:\Users\64531\Desktop\在线开单日志\1.6'  # 替换为你的文件夹路径
    # extract_gz_files(folder_path)

    # 使用示例
    folder_path = r'C:\Users\64531\Desktop\在线开单日志\1.3'  # 替换为你的文件夹路径
    search_strings = [
        'AD227FF26D771E87E053015B210A5B47',
        '2AC59CCC84B23DF7E063345B210A257C',
        '邹彦斌',
        '371002198909151016',
        '13806318956'
    ]

    search_strings_in_logs(folder_path, search_strings)












